<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Galleryadm extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 */
	public function index(){
		$this->mng_gallery();
	}
	
	//----------------------GALLERY-----------------------//-------------------------------------->GALLERY{
	function mng_gallery($mode=""){ 
		if($mode=="update"){
			$pesan="Album berhasil diperbaharui";
		}elseif($mode=="delete"){
			$pesan="Album berhasil dihapus";
		}elseif($mode=="error"){
			$pesan="Terjadi Kesalahan Silahkan Ulangi..!";
		}else{
			$pesan="";	
		}
		
		$this->load->library('pagination');
		$this->load->model('tbl_album_gambar');
	    $banyak = $this->tbl_album_gambar->hitung_album();
		$base_url = base_url();
		$config['base_url'] = $base_url.'paneladm/galleryadm/mng_gallery/';
		$config['total_rows'] = $banyak;
		$config['per_page'] = '10'; 
		$this->pagination->initialize($config);
		$data['pil'] ="semua";
		$data['mode']  = $mode;
		$data['pesan']  = $pesan;
		$data['jumlah'] =$banyak;
		$data['query'] = $this->tbl_album_gambar->load_album();
		$this->load->view('admin/galleryadm/admin-mng-gallery', $data);		 
	}
	
	function fo_upload(){
		$nama 		= $this->input->post('nama_foto');
		$deskripsi  = $this->input->post('deskripsi_foto');
		$album_id	= $this->input->post('album_foto');
		$slideshow	= $this->input->post('slideshow');
		if (($nama=="") OR ($deskripsi=="") OR ($album_id=="")){
			 redirect('paneladm/galleryadm/mng_gallery/error'); 
		 }
		
		$this->load->helper('url');
		$this->load->model('tbl_foto');
		$config['upload_path'] = './utility/pictures/';
		$config['allowed_types'] = 'tif|gif|jpg|png|bmp';
		$config['max_size']	= '2000';

		
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload())
		{
			 redirect('paneladm/galleryadm/mng_gallery/error');
		}	
		else
		{
			$file = $this->upload->data();
			$org_name = $file['file_name'];
			$size_name= $file['file_size'];
			$url = base_url()."utility/pictures/".$org_name;
			$this->tbl_foto->add_foto($nama, $deskripsi, $url, $org_name, $size_name, $album_id, $slideshow);
			$data = array('upload_foto' => $this->upload->data(),'mode' => "foto", 'nama' =>$nama, 'album_id' =>$album_id);
			$this->load->view('admin/galleryadm/admin-sukses-upload', $data);
		}
	}
	
	function make_album(){ 
		 $nama 		= $this->input->post('nama_album');
		 $deskripsi = $this->input->post('deskripsi_album');
		 if (($nama=="") OR ($deskripsi=="")){
			 redirect('paneladm/galleryadm/mng_gallery/error'); 
		 }
			 
		$this->load->helper('url');
		$this->load->model('tbl_album_gambar');
		$config['upload_path'] = './utility/pictures/album_cover';
		$config['allowed_types'] = 'tif|bmp|gif|jpg|png';
		$config['max_size']	= '1500';		
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload())
		{
			$data = array('error' => $this->upload->display_errors(), 'pil' =>'gagal');
			$this->load->library('pagination');
			$this->load->model('tbl_album_gambar');
			$banyak = $this->tbl_album_gambar->hitung_album();
			$base_url = base_url();
			$config['base_url'] = $base_url.'paneladm/galleryadm/mng_gallery/';
			$config['total_rows'] = $banyak;
			$config['per_page'] = '10'; 
			$this->pagination->initialize($config);
			$data['jumlah'] =$banyak;
			$data['query'] = $this->tbl_album_gambar->load_album();
			$this->load->view('admin/galleryadm/admin-mng-gallery', $data);
		}	
		else
		{
			$file = $this->upload->data();
			$cover = $file['file_name'];
			$this->tbl_album_gambar->add_album($nama, $deskripsi, $cover);
			$data = array('make_album' => $this->upload->data(),'mode' => "album", 'nama' => $nama, 'deskripsi' =>$deskripsi, 'url' => $cover);
			$this->load->view('admin/galleryadm/admin-sukses-upload', $data);
		} 
	}
	
	function view_album($id){
		$this->load->library('pagination');
		$this->load->model('tbl_foto');
	    $banyak = $this->tbl_foto->hitung_foto($id);
		$base_url = base_url();
		$config['base_url'] = $base_url.'paneladm/galleryadm/view_album/'.$id.'/';
		$config['total_rows'] = $banyak;
		$config['per_page'] = '15'; 
		$config['first_link'] = 'First';
		$config['last_link'] = 'Last';
		$config['uri_segment'] = '5';
		$this->pagination->initialize($config);
		$data['jumlah'] =$banyak;
		$data['id_album'] = $id;
		$data['queryfoto'] = $this->tbl_foto->load_foto($id);
		echo $banyak;
		$this->load->view('admin/galleryadm/admin-view-album', $data);	
	}
	
	function delete_album($id){
		$this->load->model('tbl_album_gambar');
		$query = $this->tbl_album_gambar->load_foto_where_album($id); 
		foreach($query as $row){
			$org_name = $row->org_name;
			unlink("./utility/pictures/".$org_name);
		}
		$query2=$this->tbl_album_gambar->load_album_cover($id);
		foreach ($query2 as $row2)
		{
			$cover		= $row2->cover;
			unlink("./utility/pictures/album_cover/".$cover);
		}
		
		$this->tbl_album_gambar->delete_album($id); 
		$this->tbl_album_gambar->delete_foto_where_album($id); 
		redirect('paneladm/galleryadm/mng_gallery/delete');
	}
	
	function delete_foto($id, $id_album){
		$this->load->model('tbl_foto');
		$org_name = $this->tbl_foto->org_name_foto($id); 
		$this->tbl_foto->delete_foto($id); 
		unlink("./utility/pictures/".$org_name);
		redirect('paneladm/galleryadm/view_album/'.$id_album);
	}
	
	function edit_foto($id) {
	    $this->load->model('tbl_foto');
		$data['query'] =$this->db->query("SELECT * FROM tbl_foto WHERE id=$id");
        $this->load->view('admin/galleryadm/admin-edit-foto', $data);
    }
	
	function edit_info_album($id) {
	    $this->load->model('tbl_album_gambar');
		$data['query'] =$this->db->query("SELECT * FROM tbl_album_gambar WHERE id=$id");
        $this->load->view('admin/galleryadm/admin-edit-album', $data);
    }
	
	function update_foto(){
		 $id 		= $this->input->post('id');
		 $album_id	= $this->input->post('album_id');
		 $nama 		= $this->input->post('nama_foto');
		 $deskripsi = $this->input->post('deskripsi_foto');
		 $slideshow = $this->input->post('slideshow');
		 if (($nama=="") AND ($deskripsi=="")){
			 redirect('paneladm/galleryadm/mng_gallery/error'); 
		 }
		$this->load->model('tbl_foto');
		$this->tbl_foto->update_foto($id, $nama, $deskripsi,$slideshow);
		redirect('paneladm/galleryadm/view_album/'.$album_id);
	}
	
	function update_album(){
		 $id 		= $this->input->post('id');
		 $nama 		= $this->input->post('nama_album');
		 $deskripsi = $this->input->post('deskripsi_album');
		 if (($nama=="") AND ($deskripsi=="")){
			 redirect('paneladm/galleryadm/mng_gallery/error'); 
		 }
		$this->load->model('tbl_album_gambar');
		$this->tbl_album_gambar->update_album($id, $nama, $deskripsi);
		redirect('paneladm/galleryadm/mng_gallery/update');
	}
	
	//-------------------------------------------------------------------------------------------->Gallery}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */